package paskal.ui.components;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.swing.JSlider;
import javax.swing.plaf.SliderUI;
import javax.swing.plaf.basic.BasicSliderUI;

import paskal.ui.Style;

public class Slider extends JSlider implements Component {
	private static final long serialVersionUID = -7673376219646630136L;

	protected Thumb thumb = null;
	protected int trackSize = 2;
	protected BufferedImage trackBefore = null;
	protected BufferedImage trackAfter = null;

	protected Color trackColorBefore = null;
	protected Color trackColorAfter = null;

	@Override
	public void stylize(String selector) {
		Style.getInstance().styleThisShit(selector, this);
	}

	public Slider() {
		setOpaque(false);
		setMinimum(0);
		setMaximum(100);
		setBackground(new Color(0, 0, 0, 0));
		setCursor(new Cursor(Cursor.HAND_CURSOR));
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		super.paintComponent(g);
	}

	@Override
	public void setProperty(String key, Object value) {
		switch (key) {
		case "bounds": {
			setBounds((Rectangle) value);
			break;
		}
		case "track": {
			@SuppressWarnings("unchecked")
			Map<String, Object> track = (Map<String, Object>) value;

			if (track.get("size") != null) {
				trackSize = (int) track.get("size");
			}

			@SuppressWarnings("unchecked")
			Map<String, Object> trackBefore = (Map<String, Object>) track
					.get("before");

			Object o = trackBefore.get("color");
			if (o != null) {
				trackColorBefore = new Color(Integer.parseInt(o.toString()
						.substring(1), 16));
			}

			String path = (String) trackBefore.get("image");
			if (path != null) {
				Rectangle r = (Rectangle) trackBefore.get("position");
				if (r != null) {
					this.trackBefore = Style.getInstance().getImage(
							(String) trackBefore.get("image"), r);
				} else {
					this.trackBefore = Style.getInstance().getImage(
							(String) trackBefore.get("image"));
				}
			}

			@SuppressWarnings("unchecked")
			Map<String, Object> trackAfter = (Map<String, Object>) track
					.get("after");

			o = trackAfter.get("color");
			if (o != null) {
				trackColorAfter = new Color(Integer.parseInt(o.toString()
						.substring(1), 16));
			}

			path = (String) trackAfter.get("image");
			if (path != null) {
				Rectangle r = (Rectangle) trackAfter.get("position");
				if (r != null) {
					this.trackAfter = Style.getInstance().getImage(
							(String) trackAfter.get("image"), r);
				} else {
					this.trackAfter = Style.getInstance().getImage(
							(String) trackAfter.get("image"));
				}
			}
			break;
		}
		case "thumb": {
			@SuppressWarnings("unchecked")
			Map<String, Object> thumb = (Map<String, Object>) value;
			this.thumb = new Thumb();
			Style.getInstance().styleThisShit(thumb, this.thumb);
			addMouseListener(this.thumb);
			break;
		}
		default:
			break;
		}
		setUI(getOwnUI());
	}

	protected SliderUI getOwnUI() {
		return new BasicSliderUI(this) {
			@Override
			public void paintTrack(Graphics g) {
				if ((trackBefore != null) && (trackAfter != null)) {
					g.setColor(new Color(0x000000));
					g.drawImage(trackAfter, trackRect.x, trackRect.y
							+ (trackRect.height - trackSize) / 2, thumbRect.x,
							trackSize, null);

					g.drawImage(trackBefore, trackRect.x, trackRect.y
							+ (trackRect.height - trackSize) / 2, thumbRect.x,
							trackSize, null);
				} else if ((trackColorAfter != null)
						&& (trackColorBefore != null)) {
					// g.setColor(getBackground());
					// g.fillRect(0, 0, getWidth(), getHeight());
					g.setColor(trackColorAfter);
					g.fillRect(trackRect.x, trackRect.y
							+ (trackRect.height - trackSize) / 2,
							trackRect.width, trackSize);
					g.setColor(trackColorBefore);
					g.fillRect(trackRect.x, trackRect.y
							+ (trackRect.height - trackSize) / 2, thumbRect.x,
							trackSize);
				} else {
					super.paintTrack(g);
				}
			}

			@Override
			public void paintThumb(Graphics g2) {
				if (thumb != null) {
					Graphics2D g = (Graphics2D) g2;
					g.drawImage(thumb.getCurrentIcon(), thumbRect.x,
							thumbRect.y
									+ (thumbRect.height - thumb
											.getCurrentIcon().getHeight()) / 2,
							null);
				} else {
					super.paintThumb(g2);
				}

			}

			@Override
			public void paintFocus(Graphics g) {
			}

			@Override
			public void paintLabels(Graphics g) {
			}

			@Override
			public void paintTicks(Graphics g) {
			}
		};
	}
}
